//
//  PersonalInformationViewController.swift
//  IDF
//
//  Created by 陈徐挺 on 27/03/2018.
//  Copyright © 2018 Jesse.chen. All rights reserved.
//

import UIKit

class WorkingInformationViewController: BaseViewController {
    
    let formData = [
        ["title": "<span style=\"font-family: PingFang SC; font-size:14px;color: #888888\"><span style=\"color: #D0021B\">*</span>Work title</span>"],
        ["title": "<span style=\"font-family: PingFang SC; font-size:14px;color: #888888\"><span style=\"color: #D0021B\">*</span>Company name</span>"],
        ["title": "<span style=\"font-family: PingFang SC; font-size:14px;color: #888888\"><span style=\"color: #D0021B\">*</span>Company province</span>"],
        ["title": "<span style=\"font-family: PingFang SC; font-size:14px;color: #888888\"><span style=\"color: #D0021B\">*</span>address</span>"],
        ["title": "<span style=\"font-family: PingFang SC; font-size:14px;color: #888888\"><span style=\"color: #D0021B\">*</span>Company phone number</span>"],
//        ["title": "<span style=\"font-family: PingFang SC; font-size:14px;color: #888888\"><span style=\"color: #D0021B\"></span>Monthly income</span>"],
    ]
    @IBOutlet weak var saveButtonView: FooterButtonView!
    @IBOutlet weak var baseTableView: UITableView!
    var tfArr: [UITextField] = Array(repeating: UITextField(), count: 13)
    var userWorkingInfo: UserWorkingInfo!
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Working information"
        baseTableView.register(cellType: InputTableViewCell.self)
        baseTableView.tableFooterView = UIView()
        // Do any additional setup after loading the view.
        saveButtonView.doneButton.setBlockFor(.touchUpInside) {[weak self] (button) in
            EZAlertController.alert("Save information", message: "Whether to save the modified information？", buttons: ["Give up", "Save"], tapBlock: { (_, index) in
                if index == 1 {
                    let _ = self?.userWorkingInfo.save().then { _ -> Void in
                        nowNavgationController.popViewController(animated: true)
                    }
                }
            })
        }
    }
    
    func saveData(_ tf: UITextField) {
        
        switch tf.tag  {
        case 0:
            userWorkingInfo.typeOfWork = tf.text!
        case 1:
            userWorkingInfo.companyName = tf.text!
        case 2:
            if let selection = getSelection(key: "province", selectText: tf.text!) {
                userWorkingInfo.companyProvince = selection.selectValue
            }
        case 3:
            userWorkingInfo.address = tf.text!
        case 4:
            userWorkingInfo.companyPhone = tf.text!
        case 5:
            if let selection = getSelection(key: "monthincome", selectText: tf.text!) {
                userWorkingInfo.monthIncome = selection.selectValue
            }
        default:
            break
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
extension WorkingInformationViewController: UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return formData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: InputTableViewCell = tableView.dequeueReusableCell(for: indexPath)
        let htmlText = formData[indexPath.row]["title"]
        cell.fieldLabel.attributedText = htmlText?.html2AttributedString
        cell.fieldTF.text = ""
        cell.fieldTF.delegate = self
        cell.fieldTF.tag = indexPath.row
        cell.accessoryType = .none
        switch indexPath.row  {
        case 0:
            cell.fieldTF.text = userWorkingInfo.typeOfWork
        case 1:
            cell.fieldTF.text = userWorkingInfo.companyName
        case 2:
            cell.fieldTF.isEnabled = false
            cell.selection = getSelection(key: "province", selectValue: userWorkingInfo.companyProvince)
            if let s = cell.selection {
                cell.fieldTF.text = s.selectText
            }
            cell.accessoryType = .disclosureIndicator
        case 3:
            cell.fieldTF.text = userWorkingInfo.address
        case 4:
            cell.fieldTF.text = userWorkingInfo.companyPhone
        case 5:
            cell.fieldTF.isEnabled = false
            cell.selection = getSelection(key: "monthincome", selectValue: userWorkingInfo.monthIncome)
            if let s = cell.selection {
                cell.fieldTF.text = s.selectText
            }
            cell.accessoryType = .disclosureIndicator
        default:
            break
        }
        tfArr[indexPath.row] = cell.fieldTF
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let cell = tableView.cellForRow(at: indexPath) as! InputTableViewCell
        cell.fieldTF.isEnabled = true
        cell.fieldTF.becomeFirstResponder()
        func setOptions(_ string: String) {
            let arr = appSelection.filter { (selection) -> Bool in
                return selection.selectType == string
            }
            let opt = OptionsView(frame: appWindow.bounds)
            opt.setWithOptions(arr)
            appWindow.addSubview(opt)
            opt.show()
            opt.clickCallback = { [weak self] section -> Void in
                cell.selection = section
                cell.fieldTF.text = section.selectText
                self?.saveData(cell.fieldTF)
            }
        }
        switch indexPath.row {
        case 5:
            cell.fieldTF.resignFirstResponder()
            cell.fieldTF.isEnabled = false
            setOptions("monthincome")
        case 2:
            cell.fieldTF.resignFirstResponder()
            cell.fieldTF.isEnabled = false
            setOptions("province")
        default:
            break
        }
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 66
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        saveData(textField)
    }
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        saveData(textField)
        return true
    }
}

